package com.mcxx.redis.service;

import cn.hutool.core.util.ObjectUtil;
import com.mcxx.redis.listener.SocietyRedisListerer;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import com.mcxx.redis.constant.RedisGlobalConstant;

import java.util.Map;



@Slf4j
@Component
@Data
public class RedisReceiver implements SocietyRedisListerer {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private RedisTemplate redisTemplate;


    public void onMessage(Map<String, Object> params) {
        Object handlerName = params.get(RedisGlobalConstant.HANDLER_NAME);
        if(null == handlerName) {
            log.error("没有指定Redis消息处理监听对象");
            return;
        }
        SocietyRedisListerer messageListener = applicationContext.getBean(handlerName.toString(), SocietyRedisListerer.class);
        if(ObjectUtil.isNotEmpty(messageListener)) {
            params.remove(RedisGlobalConstant.HANDLER_NAME);
            messageListener.onMessage(params);
        }
    }

}
